from django.http import JsonResponse
from web.models import Users
from web.utils import Utils
from django import forms
from web.apis import module
from django.core.paginator import Paginator
from django.db.models import Q

tools = Utils()

module = module.Module()

class UserForm(forms.ModelForm):
    class Meta:
        model = Users
        fields = ['username', 'password','avatar']



# 编辑用户信息
def edit_user_info(request):
    user_id = request.POST.get('user_id')
    user = Users.objects.filter(user_id=user_id).first()
    if not user:
        return JsonResponse({'code': 400, 'message': '用户不存在'})
    password = request.POST.get('password')
    
    # 如果没有密码，就主动在 request.post中添加密码
    if not password:
        request.POST = tools.QueryDict_Add(request.POST, 'password', user.password)
    elif password == '' or len(password) <= 0:
        request.POST = tools.QueryDict_Add(request.POST, 'password', user.password)
    
    form = UserForm(request.POST, instance=user, files=request.FILES)
    if form.is_valid():
        form.save()
        new_user = module.user_data_format(user)
        return JsonResponse({'code': 200, 'message': '编辑成功', 'data': new_user})
    
    error = tools.Forms_GetError(form)
    return JsonResponse({'code': 400, 'message': error[1], 'field': error[0]})

# 获取用户信息
def get_user_info(request):
    user_id = request.POST.get('user_id')
    user = Users.objects.filter(user_id=user_id).first()
    if not user:
        return JsonResponse({'code': 400, 'message': '用户不存在'})
    new_user = module.user_data_format(user)
    return JsonResponse({'code': 200, 'message': '获取成功', 'data': new_user})



# 用户列表
def get_user_list(request):
    page = request.POST.get('page', 1)
    page_size = request.POST.get('page_size', 10)
    # 判断 page 或者 page_size 是否为空和是否不是数字
    if not page or not page.isdigit():
        page = 1
    if not page_size or not page_size.isdigit():
        page_size = 10
    users = Users.objects.all().order_by('-create_time')
    paginator = Paginator(users, page_size)
    page_obj = paginator.page(page)
    new_users = []
    for user in page_obj.object_list:
        new_users.append(module.user_data_format(user))
    result = {
        'users': new_users,
        'total': paginator.count,
        'page': page,
        'page_size': page_size
    }
    return JsonResponse({'code': 200, 'message': '获取成功', 'data': result})


# 搜索用户
def search_user(request):
    # 可以搜索用户名、账号、用户ID
    keyword = request.POST.get('keyword')
    users = Users.objects.filter(Q(username__icontains=keyword) | Q(account__icontains=keyword) | Q(user_id__icontains=keyword)).order_by('-create_time')
    new_users = []
    for user in users:
        new_users.append(module.user_data_format(user))
    return JsonResponse({'code': 200, 'message': '获取成功', 'data': new_users})

# 删除用户
def delete_user(request):
    user_id = request.POST.get('user_id')
    user = Users.objects.filter(user_id=user_id).first()
    if not user:
        return JsonResponse({'code': 400, 'message': '用户不存在'})
    user.delete()
    return JsonResponse({'code': 200, 'message': '删除成功'})


